articles

home / developersection / articles / expandable treeview node in c#

Expandable TreeView Node in C#

zhang wei 14370 02-Apr-2012

In this article I’m going to explain how to make expandable treeview node in C# when others node is closed. Here I’m just creating a method to perform this task with name TreeView1_TreeNodeExpanded. Let’s take a look of this method code:

Foreground code:

<asp:TreeView ID="TreeView1" runat="server" ExpandDepth="0" OnTreeNodeExpanded="TreeView1_TreeNodeExpanded" ShowLines="True">

Code-behind:
protected void TreeView1_TreeNodeExpanded(object sender, TreeNodeEventArgs e)
         {
              TreeNodeCollection ts = null;
             if (e.Node.Parent == null)
             {
                 ts = ((TreeView)sender).Nodes;
             }
             else
                 ts = e.Node.Parent.ChildNodes;
             foreach (TreeNode node in ts)
             {
                 if (node != e.Node)
                 {
                     node.Collapse();
                 }
             }
         }

  Thanks for shwing your interest in this article.



Updated 11-Jun-2018
zhang wei

Other

Hello World


Message

Leave Comment

2 Comments

Comments

Liked By